#ifndef NETWORKTASK_H
#define NETWORKTASK_H

#pragma once

#include <QObject>
#include <QThread>
#include <QNetworkReply>
#include "networkscan.h"
#include "logpostback.h"

namespace ns_yht
{
    class NetworkTask : public QObject
    {
        Q_OBJECT
    public:
        NetworkTask(const QString &softwareCode);
        ~NetworkTask();

        void startScan();
        void stopScan();

        bool postlog_alert(int mtype, qint64 mtime, int minfo);
        bool postlog_system(int mtype, qint64 mtime);
        void postHeartbeat();

        void setShipType(int shipType);

    private slots:
        void onNetworkStatusChanged(bool state);

        void onBaseDataUpdateFinished(bool result);
        void onUpdateScanFinished();
        void onDownloadFinished();
        void onUpdateFailed();
        void onpostlogFinished();

    signals:
        void signalToStartScan();
        void signalToStopScan();
        void tokenError(int errid, QString errmsg = "");
        void signalToPostlog_alert(int mtype, qint64 mtime, int minfo);
        void signalToPostlog_system(int mtype, qint64 mtime);
        void signalToPostHeartbeat();

    private:
        void init();
        void reboot();
        void tokenScan();
        void onLoginTokenFinished(QNetworkReply *reply);
        void onTokenReceived();
        void ensureUpdateUtil();
        void postPrelog();
        QThread m_networkScanThread;
        NetworkScan *m_networkScan;
        LogPostback *m_logPostback;

        bool m_justRebooted = false;
        int m_rebootTimes = 0;
        QString m_softwareCode;
    };

}
#endif